package com.ejar.springboot_manage.controller;

import com.ejar.springboot_manage.entity.User;
import com.ejar.springboot_manage.service.UserService;
import com.ejar.springboot_manage.util.Consts;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class UserController {
    @Autowired
    private UserService userService;

//    @GetMapping("/index")
//    public ModelAndView allUser(ModelAndView model){
//        List<User> user = userService.allUser();
//        model.addObject("users",user);
//        model.setViewName("/index");
//        return model;
//    }


    @DeleteMapping("/delete/{id}")
    public Object deleteUser(@PathVariable("id") Integer id){
        userService.delete(id);
        return "redirect:/";
    }

    @GetMapping("/findById")
    public Object findById(HttpServletRequest request){
        String id = request.getParameter("id").trim();
        return userService.selectByPrimaryKey(Integer.parseInt(id));
    }


    @PostMapping("/addUser")
    public Map<String,Object> addUser(HttpServletRequest request) {
        Map resultMap = new HashMap();
        String name = request.getParameter("name").trim();
        String sex = request.getParameter("sex").trim();
        String work = request.getParameter("work").trim();

        User user = new User();
        user.setName(name);
        user.setSex(new Byte(sex));
        user.setWork(work);

        boolean flag = userService.insert(user);
        if (flag){  //成功
            resultMap.put(Consts.CODE,1);
            resultMap.put(Consts.MSG,"添加成功");
            return resultMap;
        }

        resultMap.put(Consts.CODE,0);
        resultMap.put(Consts.MSG,"添加失败");
        return resultMap;
    }

    @RequestMapping("updateUser")
    public Map<String,Object> updateUser(HttpServletRequest request){
        Map resultMap = new HashMap();
        String id = request.getParameter("id").trim();
        String name = request.getParameter("name").trim();
        String sex = request.getParameter("sex").trim();
        String work = request.getParameter("work").trim();
        User user = new User();
        user.setId(Integer.valueOf(id));
        user.setName(name);
        user.setSex(new Byte(sex));
        user.setWork(work);

        boolean flag = userService.update(user);
        if (flag){  //成功
            resultMap.put(Consts.CODE,1);
            resultMap.put(Consts.MSG,"修改成功");
            return resultMap;
        }

        resultMap.put(Consts.CODE,0);
        resultMap.put(Consts.MSG,"修改失败");
        return resultMap;

    }

}
